package com.ysc.plugin.utility.receiver

import android.content.Context
import android.content.Intent
import android.widget.Toast
import com.ysc.plugin.common.abstract.PluginLocalReceiver
import com.ysc.plugin.common.callback.BroadcastCallBack
import com.ysc.plugin.utility.network.NetWorkManager
import com.ysc.plugin.utility.photo.PhotoManger
import java.lang.reflect.Method

class UtilityReceiver(callBack: BroadcastCallBack): PluginLocalReceiver() {
    private var mCallBack:BroadcastCallBack = callBack
    override fun onReceive(context: Context?, intent: Intent?) {
        var command = intent?.extras?.get("command")
        var data:Any? = ""
        when(command.toString()){
            "photo" -> {
                var category = intent?.extras?.get("category")
                if(category == null){
                    Toast.makeText(context,"命令参数category为null",Toast.LENGTH_LONG).show()
                    return
                }
                var module = PhotoManger.getInstance()?.getManagerModuleByName(category.toString())
                var operate = intent?.extras?.get("operate")
                var parameter = intent?.extras?.get("parameter")
                if(operate == null){
                    Toast.makeText(context,"命令参数operate为null",Toast.LENGTH_LONG).show()
                    return
                }
                var method: Method? = null
                if(parameter!=null){
                    method = module?.javaClass?.getMethod(operate.toString(), java.lang.String().javaClass)
                    data = method?.invoke(module,parameter)!!
                }else
                {
                    module?.javaClass?.getMethod(operate.toString())
                    data = method?.invoke(module)!!
                }
            }
            "network"-> {
                var category = intent?.extras?.get("category")
                if(category == null){
                    Toast.makeText(context,"命令参数category为null",Toast.LENGTH_LONG).show()
                    return
                }
                var operate = intent?.extras?.get("operate")
                var method = NetWorkManager.getInstance()?.javaClass?.getMethod(operate.toString())
                data = method?.invoke(NetWorkManager.getInstance())!!
            }
        }
        if(data==null){
            data = ""
        }
        mCallBack.onExecuteFinished(data.toString())
    }
}